$(function(){
	var usename = localStorage.getItem("name");
	var gou_num = 0;
	var gou_sum = 0;
	$.ajax({
	  type: "get",
	   url: "http://datainfo.duapp.com/shopdata/getCar.php",
	   data: {userID:usename},
	   dataType:"jsonp",
	   success:function(data){
//	   	console.log(data);
	   	var gouHtml = "";
	     $.each(data, function(index){
	     	gou_num+=parseInt(data[index].number);
	     	gou_sum+=parseInt(data[index].number)*parseFloat(data[index].price);
	     	
	     	gouHtml+='<dl class="gouwu_car" goodsId='+data[index].goodsID+'>'
			gouHtml+='<dt class="gouwu_img"><img src='+data[index].goodsListImg+' alt="" /></dt>'
			gouHtml+='<dd class="gouwu_mess">'
			gouHtml+='<p class="gouwu_tit">'+data[index].goodsName+'</p>'
			gouHtml+='<p class="gouwu_type">'+data[index].className+'</p>'
			gouHtml+='<p>单价 : <span class="gouwu_pri">￥<b class="gouwu_price">'+data[index].price+'</b></span></p>'
			gouHtml+='<p>数量 : <span class="gouwu_jian">-</span><input type="text"  class="gouwu_num" value="'+data[index].number+'"/><span class="gouwu_jia">+</span></p>'
			gouHtml+='<div class="dele"><i class="iconfont del">&#xe68d;</i></div>'
			gouHtml+='</dd>'
			gouHtml+='</dl>'
	    
	    
	     });
	     
	     $(".gouwu_carbg").html(gouHtml);
	     $(".car_sum").html(gou_num);
	     $(".car_mon").html(gou_sum);
	     if(gou_num){
	     	$(".car_num1").css("display","block").html(gou_num);
	     }else{
	     	$(".car_num1").css("display","none")
	     }
	     
//	     localStorage.setItem("gou_num",gou_num)
	     /////////////////点击减号
	     $(".gouwu_jian").on("touchend",function(){
	     	var jjinde = $(".gouwu_jian").index(this);
	     	var jjsum = $(".gouwu_num").eq(jjinde).val();
	     	gou_sum = parseFloat($(".car_mon").html());
	     	var goodsId = $(".gouwu_car").eq(jjinde).attr("goodsId");
	     	i=0;
	     	if(jjsum==1){
	     		jjsum==1
	     	}else{
	     		jjsum--;
	     		i++;
	     		gou_num--;
	     		gou_sum -= i*parseFloat($(".gouwu_price").eq(jjinde).text()); 
	     		$(".gouwu_num").eq(jjinde).val(jjsum);
	     		$(".car_sum").html(gou_num);
	     		$(".car_mon").html(gou_sum);
	     		$(".car_num1").html(gou_num);
	     		
	     		updateCar(usename,goodsId,jjsum)
	     	}
	     	
	     })
	     /////////////////点击加号
	     $(".gouwu_jia").on("touchend",function(){
	     	var jjinde = $(".gouwu_jia").index(this);
	     	var jasum = $(".gouwu_num").eq(jjinde).val();
	     	gou_sum = parseFloat($(".car_mon").html());
	     	var goodsId = $(".gouwu_car").eq(jjinde).attr("goodsId");
	     	var i=0;
	     		jasum++;
	     		gou_num++;
	     		i++;
	     		gou_sum += i*parseFloat($(".gouwu_price").eq(jjinde).text());
	     		$(".gouwu_num").eq(jjinde).val(jasum);
	     		$(".car_sum").html(gou_num);
	     		$(".car_mon").html(gou_sum);
	     		$(".car_num1").html(gou_num);
	     		updateCar(usename,goodsId,jasum)
	     })
	    
	    /////////////////点击删除
	     $(".dele").on("touchend",function(){
	     	var delindex = $(".dele").index(this);
	     	var desum = $(".gouwu_num").eq(delindex).val();
	     	gou_sum = parseFloat($(".car_mon").html());
	     	var goodsId = $(".gouwu_car").eq(delindex).attr("goodsId");
	     		gou_num-=desum;
	     		gou_sum -= desum*parseFloat($(".gouwu_price").eq(delindex).text());
	     		$(".car_sum").html(gou_num);
	     		$(".car_mon").html(gou_sum);
	     		$(".car_num1").html(gou_num);
	     		updateCar(usename,goodsId,0)
	     	$(".dele").eq(delindex).parent().parent().remove();
	     	if(gou_num==0){
	     	$(".car_num1").css("display","none");
	     }
	     })
	    

	   }
	});
	
	
	
	function updateCar(name,goodsID,num){
		$.ajax({
		   type: "get",
		   url: "http://datainfo.duapp.com/shopdata/updatecar.php",
		   data: {userID:name,goodsID:goodsID,number:num},
		   dataType:"json",
		   success:function(data){
		   }
		});
	}
	
	
	
	////////////////////////////////
	$(".fenlei").on("touchend",function(){
		window.open("shoplist.html","_self");
	})
	////////////////////////////////
	$(".gouwuche").on("touchend",function(){
		window.open("gouwuche.html","_self");
	})
	/////////////////////////////////////
	$(".wodexiu").on("touchend",function(){
		window.open("wdxiu.html","_self")
	})
	//////////////////////////////
	$(".genduo").on("touchend",function(){
		window.open("chouj.html","_self")
	})
	

	
})
